package dbexp.model;

import dbexp.model.db.DbOT;

public class Sequence extends DbObject {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private String max_value = "max";
	private String increment_by = "inc";
	private String cycle_flag = "cyc";
	private String order_flag = "o";
	private String cache_size = "ca";
	private String last_number = "ln";
	private String min_value = "min";
	private String start = "start";
	private boolean ascending = true;
	public static final String LABEL = "sekwencje";
	public static final String DEF_ICON = "icon-prop";

	public Sequence() {
		super(LABEL, DbOT.SEQUENCES);
		setIcons(DEF_ICON);
	}

	public Sequence(String name) {
		super(name, DbOT.SEQUENCE);
		setIcons(DEF_ICON);
		set("leaf", true);
	}

	public Sequence(String name, String schema) {
		this(name);
		setSchema(schema);
	}

	public String getMaxValue() {
		return getNNString(max_value, "1.0E28");
	}

	public String getIncrementBy() {
		return getNNString(increment_by, "1");
	}

	public int getIncrementByValue() {
		return getInt(increment_by);
	}

	public boolean isCycleFlag() {
		return is(cycle_flag);
	}

	public boolean isOrderFlag() {
		return is(order_flag);
	}

	public String getCycleFlag() {
		return getNNString(cycle_flag);
	}

	public String getOrderFlag() {
		return getNNString(order_flag);
	}

	public int getCacheSize() {
		return getInt(cache_size);
	}

	public int getCacheSizeValue() {
		return getInt(cache_size);
	}

	public int getLastNumber() {
		return getInt(last_number);
	}

	public String getMin() {
		return getNNString(min_value, "1");
	}

	public int getMinValue() {
		return Integer.parseInt(getNNString(min_value, "1"));
	}

	public String getStartWith() {
		return getNNString(start, "1");
	}

	public int getStartWithValue() {
		return getInt(start, 1);
	}

	public void setMaxValue(String value) {
		if (value != null && value.startsWith("999999999")) {
			value = "1.0E" + (value.length() - 1);
		}
		set(max_value, value);
	}

	public void setIncrementBy(int value) {
		set(increment_by, value);
	}

	public void setCycleFlag(boolean value) {
		set(cycle_flag, value);
	}

	public void setCycleFlag(String value) {
		set(cycle_flag, value);
	}

	public void setOrderFlag(boolean value) {
		set(order_flag, value);
	}

	public void setOrderFlag(String value) {
		set(order_flag, value);
	}

	public void setCacheSize(int value) {
		set(cache_size, value);
	}

	public void setLastNumber(int value) {
		set(last_number, value);
	}

	public void setMinValue(String value) {
		set(min_value, value);
	}

	public void setStartWith(int value) {
		set(start, value);
	}

	public boolean isAscending() {
		return ascending;
	}

	public void setAscending(boolean ascending) {
		this.ascending = ascending;
	}

	public void setSchema(String schema) {
		set("schema", schema);
	}

	public String getSchema() {
		return get("schema");
	}

	private String getQuery(String oper) {
		if (requiredFieldIsNotNull()) {
			String query = "SEQUENCE " + getObjectNameForQuery() + "\n";
			if (isNotNullString(increment_by)) {
				query += " INCREMENT BY " + getIncrementBy() + getExtEnt()
						+ "\n";
			}
			if (oper == "CREATE ") {
				if (isNotNullString(start)) {
					query += " START WITH " + getStartWith() + getExtEnt()
							+ "\n";
				} else {
					if (ascending) {
						query += " START WITH 1" + getExtEnt() + "\n";
					} else {
						query += " START WITH -1" + getExtEnt() + "\n";
					}
				}
			}
			if (isNotNullString(max_value)) {
				if (getMaxValue() != "0") {
					query += " MAXVALUE " + getMaxValue() + getExtEnt() + "\n";
				} else {
					if (ascending) {
						query += " MINVALUE 1.0E28" + getExtEnt() + "\n";
					} else {
						query += " MINVALUE -1" + getExtEnt() + "\n";
					}
				}
			} else {
				query += " NOMAXVALUE" + getExtEnt() + "\n";
			}
			if (isNotNullString(min_value)) {
				if (getMinValue() != 0) {
					query += " MINVALUE " + getMinValue() + getExtEnt() + "\n";
				} else {
					if (ascending) {
						query += " MINVALUE 1" + getExtEnt() + "\n";
					} else {
						query += " MINVALUE -1.0E28" + getExtEnt() + "\n";
					}
				}
			} else {
				query += " NOMINVALUE";
			}
			if (is(cycle_flag, false) || getCycleFlag().equals("CYCLE")) {
				query += " CYCLE" + getExtEnt() + "\n";
			} else {
				query += " NOCYCLE" + getExtEnt() + "\n";
			}
			if (isNotNull(cache_size) && isNotNull(max_value)
					&& isNotNull(min_value) && isNotNull(increment_by)) {
				if (getCacheSize() != 0) {
					try {
						double min = Double.parseDouble("" + getMin());
						double max = Double.parseDouble("" + getMaxValue());
						double cache = Double.parseDouble("" + getCacheSize());
						if (cache >= Math.ceil(max - min)
								/ Math.abs(getIncrementByValue())) {
							query += " CACHE " + getCacheSize() + getExtEnt()
									+ "\n";
						} else {
							query += " CACHE 20" + getExtEnt() + "\n";
						}
					} catch (NumberFormatException nfe) {
						query += " CACHE 20" + getExtEnt() + "\n";
					}
				} else {
					query += " CACHE 20" + getExtEnt() + "\n";
				}

			} else {
				query += " CACHE 20" + getExtEnt() + "\n";
			}
			if (is(order_flag, false) || getOrderFlag().equals("ORDER")) {
				query += " ORDER" + getExtEnt();
			} else {
				query += " NOORDER" + getExtEnt() + "\n";
			}
			return oper + query + "";
		} else {
			return oneFromDual();
		}
	}

	public String getQuery(int operation) {
		if (operation == CREATE) {
			return getQuery("CREATE ");
		} else if (operation == ALTER) {
			return getQuery("ALTER ");
		} else if (operation == DELETE) {
			return "DROP SEQUENCE " + getObjectNameForQuery() + "";
		}
		return oneFromDual();
	}

}
